﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ART.Common;

namespace ART.Admini.Users
{
    public partial class UserWorkDetail : System.Web.UI.Page
    {
        protected int Id = 0;
        protected int UId = 0;
        private ART.BLL.B_UserWork bll = new ART.BLL.B_UserWork();
        private ART.Model.M_UserWork model = new ART.Model.M_UserWork();
        private BLL.B_dictionary bll_dic = new BLL.B_dictionary();

        protected void Page_Load(object sender, EventArgs e)
        {
            Id = Common.LYRequest.GetInt("id", 0);
            UId = LYRequest.GetInt("UId", 0);

#warning BLL.B_TBAdmin.IsPermissions(0x6c)

            if (!IsPostBack)
            {
                InitData();
            }
        }
        private void InitData()
        {
            JBind.BindList(dropAreadIdY, bll_dic.GetList("parentid=3").Tables[0], "DictionaryName", "id");
            dropAreadIdY.Items.Insert(0, new ListItem("请选择", "0"));

            if (Id > 0)
            {//edit
                model = bll.GetModel(Id);
                if (model != null)
                {
                    txtstartDate.Text = model.startDate.ToString();
                    txtEndDate.Text = model.EndDate.ToString();
                    txtComanyName.Text = model.ComanyName;
                    txtPositionName.Text = model.PositionName;
                    if (model.AreaId!=0)
                    {
                        Model.M_dictionary modelDic = bll_dic.GetModel(model.AreaId);
                        if (modelDic.ParentId == 0) //一级
                        {
                            this.dropAreadIdY.SelectedValue = model.AreaId.ToString();
                        }
                        else if (bll_dic.GetModel(modelDic.ParentId).ParentId == 0) //二级
                        {
                            JBind.BindList(dropAreadIdY2, bll_dic.GetList("parentid=" + modelDic.ParentId).Tables[0], "DictionaryName", "id");
                            dropAreadIdY2.Items.Insert(0, new ListItem("请选择市", "0"));
                            this.dropAreadIdY2.SelectedValue = modelDic.Id.ToString();
                            this.dropAreadIdY.SelectedValue = modelDic.ParentId.ToString();
                        }
                    }
                    //txtImgs.Text = model.Imgs;
                    txtRemarks.Text = model.Remarks;
                    txtSort.Text = model.Sort.ToString();
                }
            }
        }
        protected void dropAreadIdY_SelectedIndexChanged(object sender, EventArgs p1)
        {
            if (this.dropAreadIdY.SelectedValue != "0")
            {
                dropAreadIdY2.Items.Clear();
                JBind.BindList(dropAreadIdY2, bll_dic.GetList("parentid=" + this.dropAreadIdY.SelectedValue).Tables[0], "DictionaryName", "id");
                dropAreadIdY2.Items.Insert(0, new ListItem("请选择", "0"));
            }
        }
        protected void btnSave_Click(object sender, EventArgs p1)
        {
            if (Id > 0)
            {
                #region __________Edit__________
                model = bll.GetModel(Id);
                model.startDate = DateTime.Parse(txtstartDate.Text);
                model.EndDate = DateTime.Parse(txtEndDate.Text);
                model.ComanyName = txtComanyName.Text;
                model.PositionName = txtPositionName.Text;
                if (this.dropAreadIdY2.SelectedValue == "0")
                {
                    model.AreaId = int.Parse(this.dropAreadIdY.SelectedValue);
                }
                else
                {
                    model.AreaId =int.Parse(this.dropAreadIdY2.SelectedValue);
                }
                //model.Imgs = txtImgs.Text;
                model.Remarks = txtRemarks.Text;
                model.Sort = int.Parse(txtSort.Text);
                if (bll.Update(model))
                {
                    //B_operationLog.CreateOperationLog(B_operationLog.operationLogType.Edit, "", "");
                    MessageBox.JSLoad(sender, "alert('保存成功!');reloadDlg(true);");
                }
                #endregion
            }
            else
            {
                #region __________Add__________
                model.CreateTime = DateTime.Now;
                model.UId =UId;
                model.startDate = DateTime.Parse(txtstartDate.Text);
                model.EndDate = DateTime.Parse(txtEndDate.Text);
                model.ComanyName = txtComanyName.Text;
                model.PositionName = txtPositionName.Text;
                if (this.dropAreadIdY2.SelectedValue == "0")
                {
                    model.AreaId = int.Parse(this.dropAreadIdY.SelectedValue);
                }
                else
                {
                    model.AreaId = int.Parse(this.dropAreadIdY2.SelectedValue);
                }
                //model.Imgs = txtImgs.Text;
                model.Remarks = txtRemarks.Text;
                model.Sort = int.Parse(txtSort.Text);
                if (bll.Add(model) > 0)
                {
                    //B_operationLog.CreateOperationLog(B_operationLog.operationLogType.Add, "", "");
                    MessageBox.JSLoad(sender, "alert('保存成功!');reloadDlg(true);");
                }
                #endregion
            }
        }
    }
}
